package org.chnxi.study.redis.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.RedisClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
import org.redisson.config.SubscriptionMode;
import org.redisson.config.TransportMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.File;
import java.io.Serializable;

@Configuration
public class RedisConfig {

//    @Bean
//    public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory factory){
//        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
//        redisTemplate.setConnectionFactory(factory);
//        return redisTemplate;
//    }

    @Autowired
    private RedisProperties redisProperties;

    //@Bean
    public RedissonClient singleClient(){
        Config config = new Config();
        config.setLockWatchdogTimeout(10*1000l);
        config.setCodec(new StringCodec());
        config.useSingleServer()
                .setAddress("redis://192.168.50.227:7617");
        return Redisson.create(config);
    }

    //@Bean
    public RedissonClient sentinelClient(){
        Config config = new Config();
        config.setCodec(new StringCodec());
        config.useSentinelServers()
                .addSentinelAddress("redis://192.168.50.227:26380")
                .setMasterName("mymaster01").setCheckSentinelsList(false);
        return Redisson.create(config);

    }

    @Bean
    public RedissonClient redissonClient(){



        RedissonClient redissonClient;

        Config config = new Config();
        //config.setTransportMode(TransportMode.EPOLL);

        config.setCodec(new StringCodec());
        config.setLockWatchdogTimeout(10*1000l);

        String url = "redis://" + redisProperties.getHost() + ":" + redisProperties.getPort();
        config.useClusterServers()
                .setScanInterval(2000)
                .setSubscriptionMode(SubscriptionMode.MASTER)
                .setSubscriptionConnectionPoolSize(128)
                .addNodeAddress("redis://192.168.50.162:6001")
                .addNodeAddress("redis://192.168.50.162:6002")
                .addNodeAddress("redis://192.168.50.148:6003")
                .addNodeAddress("redis://192.168.50.148:6004")
                .addNodeAddress("redis://192.168.50.152:6005")
                .addNodeAddress("redis://192.168.50.152:6006");

        redissonClient = Redisson.create(config);
        return redissonClient;
    }

}
